from utils.prepare_data import *
import time
from utils.model_helper import *
import os
import tensorflow as tf
import numpy as np
from sklearn import metrics
from sklearn.metrics import classification_report
import pandas as pd

x_test, y_test = load_data("../data/test_set_valid.csv", one_hot=False)
y_test = list(y_test)

def caculate_acc(soft_max_ensemble,y_test=y_test):
    acc_num=0
    y_pred=get_y_pred(soft_max_ensemble)
    for i in range(len(y_test)):
        if y_test[i]==y_pred[i]:
            acc_num+=1
    acc = acc_num/len(y_test)
    return acc

def get_y_pred(soft_max_ensemble):
    y_pred = []
    for i in soft_max_ensemble:
        y_pre = np.argmax(i)
        y_pred.append(y_pre)
    return y_pred

def get_softmax(file_name):
    softmax_list = []
    with open(file=file_name,mode="r",encoding="utf-8") as f:
        result=f.readlines()
        for l in result:
            i=list(map(float,l.split("\t")))
            softmax_list.append(i)
    return softmax_list

def get_ensemble(softmax_bert,soft_max_cnn,soft_max_adversarial_abblstm,soft_max_attn_bi_lstm):
    soft_max_ensemble = []
    for i,_ in enumerate(soft_max_cnn):
        line = []
        for j,_ in enumerate(_):
            line.append(3*softmax_bert[i][j]+1.5*soft_max_cnn[i][j]+soft_max_adversarial_abblstm[i][j]+soft_max_attn_bi_lstm[i][j])
        soft_max_ensemble.append(line)
    return soft_max_ensemble

def output(y_pred):
    index_list = range(1,len(y_test)+1)
    for i,y in enumerate(y_pred):
        y_pred[i]+=1
    df = {"index":index_list,"label_predict":y_pred}
    data = pd.DataFrame(df)
    print(data)
    data.to_csv("result.csv",header=False,index=False)

if __name__ == '__main__':
    #model1-cnn
    soft_max_cnn = get_softmax('text_cnn_title_desc_checkpoint/test_results_valid.tsv')
    cnn_acc = caculate_acc(soft_max_cnn)
    print("cnn_acc:",cnn_acc)
    y_pred = get_y_pred(soft_max_cnn)
    print(classification_report(y_test, y_pred))

    #adversarial_abblstm
    soft_max_adversarial_abblstm = get_softmax('adversarial_abblstm_checkpoint/test_results_valid.tsv')
    adversarial_abblstm_acc = caculate_acc(soft_max_adversarial_abblstm)
    print("adversarial_abblstm_acc:",adversarial_abblstm_acc)
    y_pred = get_y_pred(soft_max_adversarial_abblstm)
    print(classification_report(y_test, y_pred))

    #attn_bi_lstm
    soft_max_attn_bi_lstm = get_softmax('attn_bi_lstm_checkpoint/test_results_valid.tsv')
    attn_bi_lstm_acc = caculate_acc(soft_max_attn_bi_lstm)
    print("attn_bi_lstm_acc:",attn_bi_lstm_acc)
    y_pred = get_y_pred(soft_max_attn_bi_lstm)
    print(classification_report(y_test, y_pred))

    #bert
    softmax_bert = get_softmax('bert/test_results_valid.tsv')
    bert_acc = caculate_acc(softmax_bert)
    print("bert_acc:",bert_acc)
    y_pred = get_y_pred(softmax_bert)
    print(classification_report(y_test, y_pred))
    
    #ensemble
    soft_max_ensemble = get_ensemble(softmax_bert,soft_max_cnn,soft_max_adversarial_abblstm,soft_max_attn_bi_lstm) 

    y_pred = get_y_pred(soft_max_ensemble)

    #caculate acc
    acc = caculate_acc(soft_max_ensemble,y_test=y_test)
    print("ensemble_acc:",acc)


    from sklearn import metrics
    print("Confusion Matrix...")
    cm = metrics.confusion_matrix(y_test, y_pred)
    from sklearn.metrics import classification_report
    print(classification_report(y_test, y_pred))

    #output
    #output(y_pred)
